﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Collections.Generic;
using System.Globalization;

using CE.Global;
using CE.User;
using CE.Database;
using CE.CarInsurance;
using CE.CMI;

public partial class cmi_usermessagecmi : MyLanguageBasePage
{
    protected string _header;
    protected string _body;
    protected string _headerMsg;
    protected string _bodyMsg;

    //protected override void InitializeCulture()
    //{
    //    String selectedCulture = CE.Global.General.SetCulture();
    //    //UICulture = selectedCulture;
    //    Culture = selectedCulture;
    //    base.InitializeCulture();
    //}
    string _redirectUrl = "";

    protected void Page_Load(object sender, EventArgs e)
    {
        if (Session[CE.Global.General.UserId] == null)
        {
            string _redirectUrl = CE.Global.General.GetUrl("~/car-insurance/sign-in.aspx", Page);
            Response.Redirect(_redirectUrl);
        }
        if (!IsPostBack)
        {
            string ProviderId, CarId;
            ProviderId = "0"; CarId = "0";
            if (Session["CMIDTL"] != null)
            {
                divMsgType2.Visible = true;
                string[] val = Session["CMIDTL"].ToString().Split(',');
                ProviderId = Decode(val[0]).ToString();
                CarId = val[1].ToString();

                //Session["CMIDTL"] = null;
            }

            if (CarId != "0")
            {
                LoadCarPolicyDetails(Convert.ToInt64(CarId.ToString()), Convert.ToInt64(ProviderId.ToString()));
            }

        }
    }

    public string Decode(string sData)
    {
        try
        {
            System.Text.UTF8Encoding encoder = new System.Text.UTF8Encoding();
            System.Text.Decoder utf8Decode = encoder.GetDecoder();
            byte[] todecode_byte = Convert.FromBase64String(sData);
            int charCount = utf8Decode.GetCharCount(todecode_byte, 0, todecode_byte.Length);
            char[] decoded_char = new char[charCount];
            utf8Decode.GetChars(todecode_byte, 0, todecode_byte.Length, decoded_char, 0);
            string result = new String(decoded_char);
            return result;
        }
        catch (Exception ex)
        {
            Response.Redirect("~/");
            string result = "";
            return result;
        }
    }

    public void LoadCarPolicyDetails(Int64 CMIId, Int64 ProviderCompanyId)
    {
        CMI _cmi = new CMI();
        CMIDB _cmidb = new CMIDB();
        _cmi = _cmidb.GetCMI(CMIId);

        if (_cmi != null)
        {
            if (_cmi.ServiceType.ToString() == "1")
            {
                lblSelectService.Text = Resources.CarInsurance.Service1;
            }
            else if (_cmi.ServiceType.ToString() == "2")
            {
                lblSelectService.Text = Resources.CarInsurance.CMITax;
            }
            else if (_cmi.ServiceType.ToString() == "3")
            {
                lblSelectService.Text = Resources.CarInsurance.Tax;
            }

            lblSelectInsurer.Text = "-";
            if (_cmi.CTPLProviderCompanyId > 0) GetInsurance(_cmi.CTPLProviderCompanyId);

            lblCTPLDate.Text = "-";
            if (_cmi.CMIDate != null)
            {
                lblCTPLDate.Text = _cmi.CMIDate.Value.ToString("dd") + "/" + _cmi.CMIDate.Value.ToString("MM") + "/" + _cmi.CMIDate.Value.ToString("yyyy");
            }

            lblRegistrationDate.Text = "-";
            if (_cmi.RegistrationDate != null)
            {
                lblRegistrationDate.Text = _cmi.RegistrationDate.Value.ToString("dd") + "/" + _cmi.RegistrationDate.Value.ToString("MM") + "/" + _cmi.RegistrationDate.Value.ToString("yyyy");
            }

            if (_cmi.CMITotal > 0) lblTotalPremium.Text = _cmi.CMITotal.ToString();

            if (CultureInfo.CurrentCulture.Name != "th-TH")
            {
                lblCarModelYear.Text = _cmi.ManufactYear.ToString();
            }
            else
            {
                lblCarModelYear.Text = _cmi.ManufactYear.ToString() + " - พ.ศ. " + (_cmi.ManufactYear + 543).ToString();
            }

            string _tempDesc = _cmi.CarModel + " " + _cmi.Body + " " + _cmi.CC + "cc " + _cmi.Door.ToString() + " doors";
            lblCarMakemodel.Text = _cmi.CarMake + " " + _tempDesc;

        }
       
       

        //lblCommencementdate.Text = "";
        //if (_dtProvider.Tables[0].Rows[0]["CommenceDate"].ToString() != "")
        //{
        //    lblCommencementdate.Text = Convert.ToDateTime(_dtProvider.Tables[0].Rows[0]["CommenceDate"]).ToString("dd-MMM-yyyy");
        //}
    }

    protected void imgbtnBack_Click(object sender, EventArgs e)
    {

        _redirectUrl = CE.Global.General.GetUrl("/home.aspx", Page);
        Response.Redirect(_redirectUrl);
    }

    #region Insurance

    private void GetInsurance(Int64 CTPLProviderCompanyId)
    {
        try
        {
            MyDatabase _db = new MyDatabase();
            object objC;
            if (System.Globalization.CultureInfo.CurrentCulture.Name == "th-TH")
            {
                objC = _db.ExecuteScalarSqlStatement("SELECT THCompanyNameDescription AS CompanyName FROM tblProviderCompany WHERE ProviderCompanyId=" + CTPLProviderCompanyId.ToString());
            }
            else
            {
                objC = _db.ExecuteScalarSqlStatement("SELECT CompanyNameDescription AS CompanyName FROM tblProviderCompany WHERE ProviderCompanyId=" + CTPLProviderCompanyId.ToString());
            }

            if (objC != null)
            {
                lblSelectInsurer.Text = objC.ToString();
            }
        }
        catch (Exception ex)
        {
        }
    }

    #endregion

}
